<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

function addParams($query,$number){
	
	global $route;
	
	/* get params */
	$sex1 = UV::readMQ(strip_tags($route->POST("sexOne")));
	$sex2 = UV::readMQ(strip_tags($route->POST("sexTwo")));
	$age1 = $route->POST("ageOne");
	$age2 = $route->POST("ageTwo");
	$city1 = UV::readInt(strip_tags($route->POST("cityOne")));
	$city2 = UV::readInt(strip_tags($route->POST("cityTwo")));
	
	if ($number == 1){ // line 1 params
		if ($sex1 != "all")
			$query .= " and `sex` = '$sex1'";
		$query .=  " and `age` != '0'";
		if (count($age1) < 8)
		{
			$query .= " and (";
			foreach ($age1 as $key => $agev1)
			{
				$s = explode("-",$agev1);
				$sage = $s[0];
				$bage = $s[1];
				$query .= "(`age` between '$sage' and '$bage')";
				if ($key < count($age1)-1)
					$query .= " or ";
			}
			$query .= ")";
		}
		if ($city1 != 0)
			$query .= " and `city` = '$city1'";
	} elseif ($number == 2){ // line 2 params
		if ($sex2 != "all")
			$query .= " and `sex` = '$sex2'";
		$query .=  " and `age` != '0'";
		if (count($age2) < 8)
		{
			$query .= " and (";
			foreach ($age2 as $key2 => $agev2)
			{
				$s = explode("-",$agev2);
				$sage = $s[0];
				$bage = $s[1];
				$query .= "(`age` between '$sage' and '$bage')";
				if ($key2 < count($age2)-1)
					$query .= " or ";
			}
			$query .= ")";
		}
		
		if ($city2 != 0)
			$query .= " and `city` = '$city2'";
	}
	
	return $query;
}

function printSeries($all_peoples,$peoples1,$peoples2){
	
	$series = $series1 = $series2 = "[";
	for ($i = 0; $i < count($all_peoples); $i++)
	{
		$series .= "[$i,".$all_peoples[$i]."]";
		$series1 .= "[$i,".$peoples1[$i]."]";
		$series2 .= "[$i,".$peoples2[$i]."]";
		if ($i < count($all_peoples)-1)
		{
			$series .= ",";
			$series1 .= ",";
			$series2 .= ",";
		}
	}
	$series .= "]";
	$series1 .= "]";
	$series2 .= "]";
	
	return array($series,$series1,$series2);
}

function byDay(){
	global $db;
	global $route;
	$all_peoples = $peoples1 = $peoples2 = array();
	
	$day = strtotime(date("d.m.Y"));
	if ($route->POST("fdate"))
		$day = strtotime($route->POST("fdate").".2011");
	$next_day = $day + 86400 - 1;
	
	$general = $query1 = $query2 = "select `joined_id`,`date` from `joined` where `date` between '$day' and '$next_day'";
	
	$query1 = addParams($query1,1);
	$query2 = addParams($query2,2);
	
	$general .= " order by `date`";
	$query1 .= " order by `date`";
	$query2 .= " order by `date`";
	
	$all_joined = $db->getTable($general);
	$joined1 = $db->getTable($query1);
	$joined2 = $db->getTable($query2);
	
	for ($i=0; $i<24; $i++)
		$all_peoples[] = $peoples1[] = $peoples2[] = 0;
	
	foreach ($all_joined as $join)
		$all_peoples[date("H",$join['date'])+0]++;
	foreach ($joined1 as $join)
		$peoples1[date("H",$join['date'])+0]++;
	foreach ($joined2 as $join)
		$peoples2[date("H",$join['date'])+0]++;	
	
	$all_series = printSeries($all_peoples,$peoples1,$peoples2);
	
	return '{"general":'.$all_series[0].',"line1":'.$all_series[1].',"line2":'.$all_series[2].'}';
}

function byWeek(){
	global $db;
	global $route;
	
	$all_peoples = $peoples1 = $peoples2 = array();
	
	$monday = strtotime("last Monday");
	$sunday = $monday + 7*86400-1;
	
	$general = $query1 = $query2 = "select `joined_id`,WEEKDAY(FROM_UNIXTIME(`date`)) as `date` from `joined` where `date` between '$monday' and '$sunday'";
	
	$query1 = addParams($query1,1);
	$query2 = addParams($query2,2);
	
	$general .= " order by `date`";
	$query1 .= " order by `date`";
	$query2 .= " order by `date`";
	
	$all_joined = $db->getTable($general);
	$joined1 = $db->getTable($query1);
	$joined2 = $db->getTable($query2);
	
	for ($i=0; $i<7; $i++)
		$all_peoples[] = $peoples1[] = $peoples2[] = 0;
	
	foreach ($all_joined as $join)
		$all_peoples[$join['date']]++;
	foreach ($joined1 as $join)
		$peoples1[$join['date']]++;
	foreach ($joined2 as $join)
		$peoples2[$join['date']]++;

	$all_series = printSeries($all_peoples,$peoples1,$peoples2);
	
	return '{"general":'.$all_series[0].',"line1":'.$all_series[1].',"line2":'.$all_series[2].'}';
}

function byMonth(){
	global $db;
	global $route;
	$all_peoples = $peoples1 = $peoples2 = array();
	
	$year = date("Y");
	$month = date("m");
	$startMonth = strtotime("01.".$month.".".$year);
	
	$general = $query1 = $query2 = "select `joined_id`,`date` from `joined` where `date` > '$startMonth'";
	
	$query1 = addParams($query1,1);
	$query2 = addParams($query2,2);
	
	$general .= " order by `date`";
	$query1 .= " order by `date`";
	$query2 .= " order by `date`";
	
	$all_joined = $db->getTable($general);
	$joined1 = $db->getTable($query1);
	$joined2 = $db->getTable($query2);
	
	$daysInMonth = array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	if (ceil($year/4) == $year/4)
		$daysInMonth[1]++;
	
	for ($i=0; $i<$daysInMonth[(int)$month-1]; $i++)
		$all_peoples[] = $peoples1[] = $peoples2[] = 0;
	
	foreach ($all_joined as $join)
		$all_peoples[date("j",$join['date'])-1]++;
	foreach ($joined1 as $join)
		$peoples1[date("j",$join['date'])-1]++;
	foreach ($joined2 as $join)
		$peoples2[date("j",$join['date'])-1]++;

	$all_series = printSeries($all_peoples,$peoples1,$peoples2);
	
	return '{"general":'.$all_series[0].',"line1":'.$all_series[1].',"line2":'.$all_series[2].'}';
}

function byPeriod(){
	global $db;
	global $route;
	$all_peoples = $peoples1 = $peoples2 = array();
	
	$firstDate = UV::readMQ(strip_tags($route->POST("date1")));
	$secondDate = UV::readMQ(strip_tags($route->POST("date2")));
	$firstDate = strtotime($firstDate.".2011");
	$secondDate = strtotime($secondDate.".2011")+86400-1;
	
	$diff = $secondDate - $firstDate;
	
	$period = $diff > 7776000 ? "months" : ($diff > 172800 ? "days" : "hours");
	$step = 1;
	
	$general = $query1 = $query2 = "select `joined_id`,`date` from `joined` where `date` between '$firstDate' and '$secondDate'";
	
	$query1 = addParams($query1,1);
	$query2 = addParams($query2,2);
	
	$general .= " order by `date`";
	$query1 .= " order by `date`";
	$query2 .= " order by `date`";
	
	$all_joined = $db->getTable($general);
	$joined1 = $db->getTable($query1);
	$joined2 = $db->getTable($query2);
	
	switch ($period)
	{
		case "hours":	// reset arrays
						for ($i = 0; $i < ceil($diff/3600); $i++)
							$all_peoples[] = $peoples1[] = $peoples2[] = 0;
						// fill arrays
						foreach ($all_joined as $join)
						{
							$factor = ceil(($join['date'] - $firstDate)/3600/24) - 1;
							$all_peoples[date("H",$join['date'])+$factor]++;
						}
						foreach ($joined1 as $join)
						{
							$factor = ceil(($join['date'] - $firstDate)/3600/24) - 1;
							$peoples1[date("H",$join['date'])+$factor]++;
						}
						foreach ($joined2 as $join)
						{
							$factor = ceil(($join['date'] - $firstDate)/3600/24) - 1;
							$peoples2[date("H",$join['date'])+$factor]++;
						}
						// set step
						if ($diff > 86400)
							$step = 2;
						
						break;
		case "days":	// get count selected days
						$countDays = ceil(($secondDate - $firstDate)/86400);
						// reset arrays
						for ($i = 0; $i < $countDays; $i++)
							$all_peoples[] = $peoples1[] = $peoples2[] = 0;
						// fill arrays
						foreach ($all_joined as $join)
							$all_peoples[ceil((strtotime(date("d.m.Y",$join['date']))-$firstDate)/86400)]++;
						foreach ($joined1 as $join)
							$peoples1[ceil((strtotime(date("d.m.Y",$join['date']))-$firstDate)/86400)]++;
						foreach ($joined2 as $join)
							$peoples2[ceil((strtotime(date("d.m.Y",$join['date']))-$firstDate)/86400)]++;
						
						// set step
						$step = ceil($countDays / 31);
						
						break;
		case "months":	// get count selected months
						$firstMonth = date("m",$firstDate);
						$countMonths = date("m",$secondDate) - $firstMonth;
						// reset arrays
						for ($i = 0; $i <= $countMonths; $i++)
							$all_peoples[] = $peoples1[] = $peoples2[] = 0;
						// fill arrays
						foreach ($all_joined as $join)
							$all_peoples[date("m",$join['date'])-$firstMonth]++;
						foreach ($joined1 as $join)
							$peoples1[date("m",$join['date'])-$firstMonth]++;
						foreach ($joined2 as $join)
							$peoples2[date("m",$join['date'])-$firstMonth]++;
						
						// set step
						//$step = ceil($countMonths / 31);
						
						break;
	}
	
	$all_series = printSeries($all_peoples,$peoples1,$peoples2);
	
	return '{"options":{"period":"'.$period.'","step":"'.$step.'","count":"'.count($all_peoples).'"},"general":'.$all_series[0].',"line1":'.$all_series[1].',"line2":'.$all_series[2].'}';
}

switch (UV::readMQ(strip_tags($route->POST("period"))))
{
	case "byDay":		echo byDay(); break;
	case "byWeek":		echo byWeek(); break;
	case "byMonth":		echo byMonth(); break;
	case "byPeriod":	echo byPeriod(); break;
}
?>